
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.shortcuts import render, redirect
from .forms import ProductForm, OrderForm
from .models import Product, Order
from django.contrib import messages


@login_required(login_url='user-login')
def index(request):
    form = OrderForm()
    orders = Order.objects.all()
    orders_count=orders.count()
    products = Product.objects.all()
    items = Product.objects.all()
    items_count = items.count()
    workers = User.objects.all()
    workers_count = workers.count()
    if request.method == 'POST':
        form = OrderForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.staff=request.user
            instance.save()
            return redirect('dashboard-index')
    else:
            form = OrderForm()
    context = {'form': form, 'orders':orders, 'orders_count':orders_count,'products':products,'items_count':items_count,'workers_count':workers_count}
    return render(request, 'dashboard/index.html',context)

@login_required(login_url='user-login')
def staff(request):
    workers=User.objects.all()
    workers_count=workers.count()
    context={'workers':workers, 'workers_count':workers_count,}
    return render(request, 'dashboard/staff.html',context=context)

@login_required(login_url='user-login')
def product_list(request):
    form = ProductForm()
    items = Product.objects.all()
    items_count=items.count()
    if request.method == 'POST':
        form = ProductForm(request.POST)
        if form.is_valid():
            form.save()
            product_name = form.cleaned_data.get('name')
            messages.success(request, f'你已创建{product_name}')
            return redirect('dashboard-product')
    else:
        form = ProductForm()
    context = {'form': form, 'items': items, 'items_count':items_count}
    return render(request, 'dashboard/product.html', context=context)

def staff_detail(request,pk):
    worker = User.objects.get(id=pk)
    context = {'worker': worker}
    return render(request, 'dashboard/staff_detail.html', context=context)


@login_required(login_url='user-login')
def product_delete(request, pk):
    item = Product.objects.get(id=pk)
    if request.method == 'POST':
        item.delete()
        messages.add_message(request, messages.WARNING, '删除成功！')
        return redirect('dashboard-product')
    context = {'item': item}
    return render(request, 'dashboard/product_delete.html', context)

@login_required(login_url='user-login')
def product_update(request, pk):
    item = Product.objects.get(id=pk)
    if request.method == 'POST':
        form = ProductForm(request.POST, instance=item)
        if form.is_valid():
            form.save()
            product_name = form.cleaned_data.get('name')
            messages.success(request, f'你成功创建了{product_name}')
            return redirect('dashboard-product')
    else:
        form = ProductForm()
    context = {'form': form}
    return render(request, 'dashboard/product_update.html', context)



@login_required(login_url='user-login')
def order_list(request):
    form = OrderForm()
    orders = Order.objects.all()
    orders_count=orders.count()
    if request.method == 'POST':
        form = OrderForm(request.POST)
        if form.is_valid():
            form.save()
            order_name = form.cleaned_data.get('name')
            messages.success(request, f'你已创建{order_name}')
            return redirect('dashboard-order')
    else:
        form = OrderForm()
    context = {'form': form, 'orders': orders, 'orders_count':orders_count,}
    return render(request, 'dashboard/order.html',context)

@login_required(login_url='user-login')
def order_delete(request, pk):
    order = Order.objects.get(id=pk)
    if request.method == 'POST':
        order.delete()
        messages.add_message(request, messages.WARNING, '删除成功！')
        return redirect('dashboard-index')
    context = {'order': order}
    return render(request, 'dashboard/order_delete.html', context)


@login_required(login_url='user-login')
def order_update(request, pk):
    order = Order.objects.get(id=pk)
    if request.method == 'POST':
        form = OrderForm(request.POST, instance=order)
        if form.is_valid():
            form.save()
            messages.success(request, f'你已成功变更！')
            return redirect('dashboard-index')
    else:
        form = OrderForm(instance=order)
    context = {'form': form}
    return render(request, 'dashboard/order_update.html', context)
